GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#2835)
by
unknown
06:01
created

$.fn.symphonyTimeAgo   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 29
rs 6.7272
1
/**
2
 * @package assets
3
 */
4
5
(function($, Symphony) {
6
	'use strict';
7
8
	/**
9
	 * Convert absolute to relative dates.
10
	 *
11
	 * @name $.symphonyTimeAgo
12
	 * @class
13
	 *
14
	 * @param {Object} options An object specifying containing the attributes specified below
15
	 * @param {String} [options.items='time'] Selector to find the absolute date
16
	 * @param {String} [options.timestamp='utc'] Attribute of `object.items` representing the timestamp of the given date
17
	 * @param {Integer} [options.max=0] Plugin will disable when the minutes exceed this value. By default this behaviour is off.
18
	 *
19
	 * @example
20
21
			$('.notifier').symphonyTimeAgo();
22
	 */
23
	$.fn.symphonyTimeAgo = function(options) {
24
		var objects = this,
25
			settings = {
26
				items: 'time',
27
				timestamp: 'utc',
28
				max: 0
29
			};
30
31
		$.extend(settings, options);
32
33
	/*-----------------------------------------------------------------------*/
34
35
		Symphony.Language.add({
36
			'just now': false,
37
			'a minute ago': false,
38
			'{$minutes} minutes ago': false,
39
			'about 1 hour ago': false,
40
			'about {$hours} hours ago': false
41
		});
42
43
	/*-------------------------------------------------------------------------
44
		Functions
45
	-------------------------------------------------------------------------*/
46
47
		function parse(item) {
48
			var timestamp = item.data('timestamp'),
49
				datetime;
50
51
			// Fetch stored timestamp
52
			if($.isNumeric(timestamp)) {
53
				return timestamp;
54
			}
55
56
			// Parse date
57
			else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
58
				datetime = item.attr(settings.timestamp);
59
60
				// Defined date and time
61
				if(datetime) {
62
					// Datetime will be in seconds since Epoch, JS requires
63
					// millseconds, so multiply by 1000.
64
					timestamp = new Date(datetime * 1000);
65
				}
66
67
				// Undefined date and time
68
				else {
69
					timestamp = new Date().getTime();
70
				}
71
72
				// Store and return timestamp
73
				item.data('timestamp', timestamp);
74
				return timestamp;
75
			}
76
		}
77
78
		function say(from, to) {
79
80
			// Calculate time difference
81
			var distance = to - from,
82
83
			// Convert time to minutes
84
			time = Math.floor(distance / 60000);
85
86
			// Return relative date based on passed time
87
			if(time < 1) {
88
				return Symphony.Language.get('just now');
89
			}
90
			if(time < 2) {
91
				return Symphony.Language.get('a minute ago');
92
			}
93
			if(time < 45) {
94
				return Symphony.Language.get('{$minutes} minutes ago', {
95
					'minutes': time
96
				});
97
			}
98
			if(time < 90) {
99
				return Symphony.Language.get('about 1 hour ago');
100
			}
101
			else if (!settings.max || time < settings.max) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if !settings.max || time < settings.max is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
102
				return Symphony.Language.get('about {$hours} hours ago', {
103
					'hours': Math.floor(time / 60)
104
				});
105
			}
106
		}
107
108
	/*-------------------------------------------------------------------------
109
		Initialisation
110
	-------------------------------------------------------------------------*/
111
112
		objects.find(settings.items).each(function timeago() {
113
			var item = $(this),
114
				from = parse(item),
115
				to = new Date(),
116
				rel = say(from, to);
117
118
			// Set relative time
119
			if (rel) {
120
				item.text(rel);
121
			}
122
		});
123
124
	/*-----------------------------------------------------------------------*/
125
126
		return objects;
127
	};
128
129
})(window.jQuery, window.Symphony);
130